package com.iszhouhua.blog.controller.vue;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iszhouhua.blog.common.util.Result;
import com.iszhouhua.blog.model.Article;
import com.iszhouhua.blog.model.Comment;
import com.iszhouhua.blog.model.User;
import com.iszhouhua.blog.service.CommentService;
import com.iszhouhua.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("comment")
public class CommentController {

  @Autowired
  private CommentService commentService;

  @Autowired
  private UserService userService;

  @PostMapping("add")
  public Result addComment(@RequestBody Comment comment, HttpServletRequest request){
    String userAgent = request.getHeader("user-agent");
    String ip = request.getRemoteAddr();//返回发出请求的IP地址
    comment.setUserAgent(userAgent);
    comment.setIp(ip);
    comment.setCreateTime(new Date());
    comment.setStatus(0);
    if (comment.getTargetType()== 1){
      comment.setReplyUserId(0L);
      comment.setParentId(0L);
    }
    boolean save = commentService.save(comment);
    if (!save){
      return new Result(50000,"添加失败", null);
    }
    return new Result(20000,"添加成功", null);
  }

  @GetMapping("list")
  public Result addComment(@RequestParam(value = "current", required = false, defaultValue = "1") int current,
                           @RequestParam(value = "size", required = false, defaultValue = "10000") int size,
                           @RequestParam(value = "articleId") int articleId,
                           HttpServletRequest request){

    IPage<Comment> page = new Page<>(current, size);
    QueryWrapper<Comment> queryWrapper = new QueryWrapper();
    IPage<Comment> iPage = commentService.page(page,queryWrapper.eq("status",1).eq("article_id",articleId));
    List<Comment> records = iPage.getRecords();
    for (Comment record : records) {
      //设置评论的用户信息
      User user = userService.findUserById(record.getUserId());
      user.setPassword("");
      user.setSalt("");
      record.setUser(user);
      //父级
      if (record.getParentId()!=0){
        QueryWrapper<Comment> wrapper = new QueryWrapper();
        List<Comment> commentList = commentService.list(wrapper.eq("parent_id", record.getParentId()));
        for (Comment comment : commentList) {
          User userById = userService.findUserById(comment.getReplyUserId());
          userById.setPassword("");
          userById.setSalt("");
          comment.setParentUser(userById);
        }
        record.setSubComments(commentList);
      }
      //子级
      if (record.getReplyUserId()!=0 ){
        User repalyuser = userService.findUserById(record.getReplyUserId());
        repalyuser.setPassword("");
        repalyuser.setSalt("");
        record.setReplyUser(repalyuser);
      }
    }
    return new Result(20000,"查询成功", iPage);
  }

}
